package com.dq.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dq.health.dao.user.UserBaseInfoMapper;
import com.dq.health.dao.user.UserHealthInfoMapper;
import com.dq.health.dto.request.user.QueryUserParam;
import com.dq.health.dto.request.user.SaveUserRequest;
import com.dq.health.dto.response.user.UserDetail;
import com.dq.health.entity.user.UserBaseInfo;
import com.dq.health.entity.user.UserHealthInfo;
import com.dq.health.enums.BaseStatusEnum;
import com.dq.health.service.IAPIUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * <>
 *
 * @Author: chenxin
 * @Date: 2021/5/15
 */
@Service
public class UserServiceImpl implements IAPIUserService {

    @Resource
    private UserBaseInfoMapper userBaseInfoMapper;

    @Resource
    private UserHealthInfoMapper userHealthInfoMapper;

    @Override
    public UserBaseInfo queryUserById(Long userId) {
        return userBaseInfoMapper.selectById(userId);
    }

    @Override
    public UserDetail queryUserDetail(Long userId) {
        if (userId == null) {
            return null;
        }

        UserBaseInfo userBaseInfo = userBaseInfoMapper.selectById(userId);
        if (userBaseInfo == null || userBaseInfo.getUserStatus() == BaseStatusEnum.DELETE.getValue()) {
            return null;
        }
        UserDetail userDetail = new UserDetail();
        BeanUtils.copyProperties(userBaseInfo, userDetail);

        UserHealthInfo userHealthInfo = userHealthInfoMapper
            .selectOne(new LambdaQueryWrapper<UserHealthInfo>().eq(UserHealthInfo::getUserId, userId));
        if (userHealthInfo != null) {
            BeanUtils.copyProperties(userHealthInfo, userDetail);
        }

        return userDetail;
    }

    @Override
    public UserBaseInfo queryUserByUserName(String userName) {
        if (StringUtils.isBlank(userName)) {
            return null;
        }

        UserBaseInfo userBaseInfo = userBaseInfoMapper
            .selectOne(new LambdaQueryWrapper<UserBaseInfo>().eq(UserBaseInfo::getUserName, userName));
        if (userBaseInfo == null) {
            userBaseInfo = userBaseInfoMapper
                .selectOne(new LambdaQueryWrapper<UserBaseInfo>().eq(UserBaseInfo::getMobile, userName));
        }

        return userBaseInfo;
    }

    @Override
    public UserBaseInfo queryUserByMobile(String mobile) {
        if (StringUtils.isBlank(mobile)) {
            return null;
        }

        return userBaseInfoMapper.selectOne(new LambdaQueryWrapper<UserBaseInfo>().eq(UserBaseInfo::getMobile, mobile));
    }

    @Override
    public IPage<UserDetail> queryUserList(QueryUserParam param) {
        IPage<UserBaseInfo> page = new Page<>(param.getPage(), param.getPageSize());
        List<UserDetail> userBaseInfos = userBaseInfoMapper.queryUserList(page, param);
        IPage<UserDetail> result = new Page<>();
        result.setTotal(page.getTotal());
        result.setRecords(userBaseInfos);
        return result;
    }

    @Override
    @Transactional
    public Long addUser(SaveUserRequest request) {
        Assert.notNull(request.getUserName(), "userName is cant be null");
        Assert.notNull(request.getMobile(), "userName is cant be null");
        Assert.notNull(request.getPassword(), "userName is cant be null");

        UserBaseInfo userBaseInfo = new UserBaseInfo();
        BeanUtils.copyProperties(request, userBaseInfo);
        userBaseInfoMapper.insert(userBaseInfo);

        Long userId = userBaseInfo.getUserId();
        UserHealthInfo userHealthInfo = new UserHealthInfo();
        BeanUtils.copyProperties(request, userHealthInfo);
        userHealthInfo.setUserId(userId);
        userHealthInfoMapper.insert(userHealthInfo);

        return userId;
    }

    @Override
    @Transactional
    public void updateUserInfo(SaveUserRequest request) {
        Assert.notNull(request.getUserId(), "userName is cant be null");

        UserBaseInfo updateUser = new UserBaseInfo();
        BeanUtils.copyProperties(request, updateUser);
        userBaseInfoMapper.updateById(updateUser);

        UserHealthInfo userHealthInfo = new UserHealthInfo();
        BeanUtils.copyProperties(request, userHealthInfo);
        userHealthInfoMapper.updateById(userHealthInfo);
    }

    @Override
    public void updatePassword(Long userId, String password) {
        if (userId == null || StringUtils.isBlank(password)) {
            return;
        }

        UserBaseInfo updateUser = new UserBaseInfo();
        updateUser.setUserId(userId);
        updateUser.setPassword(password);
        userBaseInfoMapper.updateById(updateUser);
    }
}
